//
//  TF_LivingAnnouncement.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/2/19.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_LivingAnnouncement: UIView , UITextViewDelegate
{
    let glView = UIView()
    let leftImageView = UIImageView()
    let rightImageView = UIImageView()
    let contentView = UIView()
    let contentLabel = UILabel()
    let close = UIButton()
    let title = UILabel()
    let content = TF_StoyCopyTextView.init()

    var gl = CAGradientLayer()
    override init(frame: CGRect) {
        super.init(frame: frame)

        self.addSubview(self.glView)
        
    
        self.glView.layer.addSublayer(self.gl)
        self.glView.layer.cornerRadius = 10;
        

        self.addSubview(self.leftImageView)
        self.leftImageView.image = UIImage.init(named: "HTLive.bundle/gonggaozuo")
        
        self.addSubview(self.rightImageView)
        self.rightImageView.backgroundColor = UIColor.clear
        self.rightImageView.image = UIImage.init(named: "HTLive.bundle/gonggaoyou")
    
        self.contentView.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
        self.addSubview(self.contentView)
        

        self.contentLabel.numberOfLines = 0

        //聊天内容

        self.content.backgroundColor = UIColor.clear
        
        
        self.content.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(red: 49/255.0, green: 166/255.0, blue: 255/255.0, alpha: 1.0)]
        
        self.content.textContainer.lineFragmentPadding = 0;
        self.content.textContainerInset =  UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0);
        self.content.font = UIFont.PingFang(size: 16)
        self.content.isScrollEnabled = false
        self.content.isEditable = false
      
  
        self.contentView.addSubview(self.content)
        
        self.close.setImage(UIImage.init(named: "HTLive.bundle/LivingClose"), for: .normal)
        
        self.addSubview(self.close)
        self.close.addTarget(self, action: #selector(closeClick), for: .touchUpInside)
      
        self.title.numberOfLines = 0;
        self.addSubview(self.title)
        self.title.font = UIFont.PingFang(size: 20)
        self.title.text = "公告";
        self.title.textAlignment = .center;
        self.title.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
        
    }
    @objc func closeClick()  {
        self.alpha = 0
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.glView.frame = self.bounds;
        self.gl.frame = self.bounds;
        self.gl.startPoint = CGPoint(x: 0.5, y: 0)
        self.gl.endPoint =  CGPoint(x: 0.5, y: 1)
        
        
        
        self.gl.colors = [UIColor(red: 107/255.0, green: 181/255.0, blue: 255/255.0, alpha: 1).cgColor, UIColor(red: 65/255.0, green: 144/255.0, blue: 235/255.0, alpha: 1).cgColor]
        
        
        self.gl.locations = [0, 1]
        
        
        self.leftImageView.frame = CGRect(x: 0, y: -5, width: 121, height: 60)
        
        self.rightImageView.frame = CGRect(x: self.frame.size.width - 121, y: -5, width: 121, height: 60)

        
        self.close.frame = CGRect(x: self.frame.size.width - 32 - 4, y:10, width: 32, height: 32)
        
        self.title.frame = CGRect(x: 0, y:15, width: self.frame.size.width, height: 28)
    }
    
    @objc public func setAnnouncement(result:TFAnnouncement)
    {
        let contentViewW = self.frame.size.width - 24;
        let contentLabelW = contentViewW - 22 - 22;
        
        
        let str = result.content
        let AttributedString = NSMutableAttributedString.init(string: str)
       
        let font:UIFont = UIFont.PingFang(size: 16)
        
    
        let contentViewStrH = TalkfunSharedTools.getSizeLabel(labelString: AttributedString, labelWidth:contentLabelW, labelfont: font)
        
        
        
        let contentViewH = contentViewStrH.height + 19.0 + 21.0
        let contentViewY = 50.0
        self.contentView.frame = CGRect(x: 12.0, y: contentViewY, width: contentViewW, height: contentViewH)
        self.contentView.layer.cornerRadius = 6

        self.content.frame = CGRect(x: 22.0, y: 19.0, width: contentLabelW, height: contentViewStrH.height)
        
        
    
        let attr: [NSAttributedString.Key : Any] = [.font: font as Any,.foregroundColor: UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1), ]
        
        let string = NSMutableAttributedString.init(string: result.content, attributes: attr)
        
   

        self.content.addHyperlink(font: font, attributedText: AttributedString)
        self.content.attributedText = string;

        let frameH = contentViewStrH.height + 19.0 + 21.0 + 46.0 + 10.0
        self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height:frameH )
        
        
    }
    
}
